package org.txazo.blog.web.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.txazo.blog.dao.MemberInfoDao;
import org.txazo.blog.util.StringUtil;
import org.txazo.blog.web.action.base.BaseAction;

/**
 * LoginAction
 * 
 * @author Tu XiaoZhou
 * @since 2013-08-10
 * 
 */
@Controller
@Scope("session")
@Namespace("/login")
public class LoginAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	/** 邮箱帐号 */
	public String account;
	/** 帐号密码 */
	public String password;
	/** 验证码 */
	private String authcode;

	/** 提示一 */
	private String tip1;
	/** 提示二 */
	private String tip2;
	/** 提示三 */
	private String tip3;

	/** 是否保存帐号 */
	public String saveAccount;
	/** 是否自动登录 */
	public String autoLogin;

	/** 自动登录跳过验证 */
	public String skipAuth;

	@Autowired
	private MemberInfoDao memberInfoDao;

	// @Autowired
	// private CoreCache coreCache;

	// @Autowired
	// private Ehcache userCache;

	/**
	 * 跳转到登录页面
	 */
	@Action(value = "index", results = { @Result(name = SUCCESS, location = "/login/login.jsp") })
	public String index() {
		// String value = (String) coreCache.get(getRemoteIP());
		// if (value == null) {
		// value = "Host" + getRemoteIP();
		// coreCache.set(getRemoteIP(), value, 60);
		// }
		// request.setAttribute("ip", value);
		// String value = null;
		// Element element = userCache.get(getRemoteIP());
		// if (element != null) {
		// value = (String) element.getValue();
		// System.out.println("value yes");
		// }
		// if (value == null) {
		// System.out.println("value no");
		// value = "Host" + getRemoteIP();
		// userCache.put(new Element(getRemoteIP(), value));
		// }
		// request.setAttribute("ip", value);

		// memberInfoDao.isEmailAccountExist("784990655@qq.com");

		return SUCCESS;
	}

	/**
	 * 登录
	 */
	@Action(value = "login", results = {
			@Result(name = SUCCESS, location = "/personal/personal.jsp"),
			@Result(name = INPUT, location = "/login/login.jsp") })
	public String login() {
		String result = SUCCESS;

		if (skipAuth != null) {
			return result;
		}

		if (StringUtil.isEmpty(account)) {
			tip1 = "请输入帐号";
			result = INPUT;
		}

		if (StringUtil.isEmpty(password)) {
			tip2 = "请输入密码";
			result = INPUT;
		}

		String validateCode = (String) session
				.getAttribute("loginValidateCode");
		if (StringUtil.isEmpty(authcode)) {
			tip3 = "请输入验证码";
			result = INPUT;
		} else if (!authcode.equals(validateCode)) {
			tip3 = "输入的验证码不正确";
			result = INPUT;
		}

		if (result.equals(INPUT)) {
			return INPUT;
		}

		// MemberInfo user = memberInfoService.login(account, password);

		// if (user == null) {
		// tip1 = "帐号和密码不正确";
		// return INPUT;
		// }
		//
		// session.setAttribute("user", user);

		// if (saveAccount != null) {
		// CookieUtil.addCookie("account", account, "/", response);
		// } else {
		// CookieUtil.clearCookie("account", request, response);
		// }
		//
		// if (autoLogin != null) {
		// CookieUtil.addCookie("account", account, "/", response);
		// CookieUtil.addCookie("password", password, "/", response);
		// } else {
		// CookieUtil.clearCookie("account", request, response);
		// CookieUtil.clearCookie("password", request, response);
		// }

		return SUCCESS;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getAuthcode() {
		return authcode;
	}

	public void setAuthcode(String authcode) {
		this.authcode = authcode;
	}

	public String getTip1() {
		return tip1;
	}

	public void setTip1(String tip1) {
		this.tip1 = tip1;
	}

	public String getTip2() {
		return tip2;
	}

	public void setTip2(String tip2) {
		this.tip2 = tip2;
	}

	public String getTip3() {
		return tip3;
	}

	public void setTip3(String tip3) {
		this.tip3 = tip3;
	}

	public String getSaveAccount() {
		return saveAccount;
	}

	public void setSaveAccount(String saveAccount) {
		this.saveAccount = saveAccount;
	}

	public String getAutoLogin() {
		return autoLogin;
	}

	public void setAutoLogin(String autoLogin) {
		this.autoLogin = autoLogin;
	}

	public String getSkipAuth() {
		return skipAuth;
	}

	public void setSkipAuth(String skipAuth) {
		this.skipAuth = skipAuth;
	}

}
